package test.my.com.myapplicationtest.bridge_webview;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.widget.Button;

import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.BridgeWebViewClient;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.github.lzyzsd.jsbridge.DefaultHandler;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import test.my.com.myapplicationtest.R;
import test.my.com.myapplicationtest.utils.ToastUtil;

/**
 * Author : Cooper
 * Time : 2017/2/3  15:42
 * Description : BridgeWebView 测试
 */

public class BridgeWebViewActivity extends Activity {

    @Bind(R.id.bridgeWebView)
    BridgeWebView bridgeWebView;
    @Bind(R.id.test_bt)
    Button testBt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_bridge_webview_layout);
        ButterKnife.bind(this);

        initBridgeWebView();
    }

    private void initBridgeWebView() {
        bridgeWebView.setDefaultHandler(new DefaultHandler());
        bridgeWebView.setWebChromeClient(new WebChromeClient());
        bridgeWebView.setWebViewClient(new MyBridgeWebViewClient(bridgeWebView));
        bridgeWebView.loadUrl("file:///android_asset/bridge.html");
        bridgeWebView.registerHandler("submitFromWeb", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                ToastUtil.showToast(data);
                function.onCallBack("这是Java返回的内容");
            }
        });
    }

    @OnClick(R.id.test_bt)
    public void onClick() {
        bridgeWebView.callHandler("functionInJs","android 调用html 方法",new CallBackFunction(){
            @Override
            public void onCallBack(String data) {
                ToastUtil.showToast(data);
            }
        });
    }

    class MyBridgeWebViewClient extends BridgeWebViewClient {

        public MyBridgeWebViewClient(BridgeWebView webView) {
            super(webView);
        }
    }
}
